package com.example.lemis.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.lemis.model.dto.DoorplateInsertDTO;
import com.example.lemis.model.dto.DoorplateQuery;
import com.example.lemis.model.dto.DoorplateSummaryQuery;
import com.example.lemis.model.dto.PageDTO;
import com.example.lemis.model.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.lemis.common.JsonResponse;
import com.example.lemis.service.IDoorplateService;
import com.example.lemis.model.domain.Doorplate;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-06-24
 * @version v1.0
 */
@RestController
@RequestMapping("/api/doorplate")
public class DoorplateController {

    private final Logger logger = LoggerFactory.getLogger( DoorplateController.class );

    @Autowired
    private IDoorplateService doorplateService;


    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse<Doorplate> getById(@PathVariable("id") Long id)throws Exception {
        Doorplate doorplate = doorplateService.getById(id);
        return JsonResponse.success(doorplate);
    }

    /**
     * 描述：管理员通过用户名查询用户门牌信息
     */
    @GetMapping("/search_admin")
    public JsonResponse doorplate_search_admin(PageDTO pageDTO, String name)throws Exception{
        Page<userDoorplateVO> _page = doorplateService.doorplate_search_admin(pageDTO, name);
        return JsonResponse.success(_page);
    }

    /**
     * 描述：管理员修改门牌状态
     */
    @PostMapping("/status")
    public JsonResponse<?> update_doorplate_status(@RequestBody Doorplate doorplate)throws Exception{
        Doorplate _doorplate = doorplateService.update_doorplate_status(doorplate);
        if (_doorplate == null) {
            return JsonResponse.failure("门牌不存在");
        }
        DoorplateStatusVO  _doorplateStatusVO = new DoorplateStatusVO();
        BeanUtils.copyProperties(_doorplate, _doorplateStatusVO);
        return JsonResponse.success(_doorplateStatusVO);
    }

    @PostMapping("")
    public JsonResponse<?> insert(@Validated @RequestBody DoorplateInsertDTO doorplate) throws Exception {
        return JsonResponse.success(doorplateService.insert(doorplate));
    }

    @GetMapping("")
    public JsonResponse<?> queryByUser(DoorplateQuery query, PageDTO pageDTO) throws Exception {
        Page<DoorplateVO> page = doorplateService.queryByUser(query, pageDTO);
        return JsonResponse.success(page);
    }

    @GetMapping("/summary")
    public JsonResponse<?> summary(DoorplateSummaryQuery query) throws Exception {
        List<DoorplateSummaryVO> doorplateVO = doorplateService.summary(query);
        return JsonResponse.success(doorplateVO);
    }
}

